package dt.inlee.common.interceptor;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * Servlet Filter implementation class SsoLogoutFilter
 */
public class SsoLogoutFilter implements Filter {

    /**CAS单点登陆注销Filter
     * Default constructor. 
     * 2012.4.26
     */
    public SsoLogoutFilter() {
        // TODO Auto-generated constructor stub
    }

	private FilterConfig filterConfig = null;
	private String logoutURL = null;
	public void init(FilterConfig config) throws ServletException {
		this.filterConfig = config;
		logoutURL = filterConfig.getInitParameter("logoutURL");  
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException, IOException {
		HttpServletRequest req = (HttpServletRequest) request;
		String path = req.getServletPath();
		String igpath = path.substring(path.lastIndexOf("/") + 1);
		//如果用户执行注销操作。则同时执行CAS的注销操作
		if (igpath.equalsIgnoreCase("exitLogin")) {
//			
			String url = "<script language='javascript'>window.top.location.href='" + logoutURL
					+ "'</script>";
			Writer writer = response.getWriter();
			writer.write(url);
			writer.flush();
			chain.doFilter(request, response);
		}
	}

	public void destroy() {
		this.filterConfig = null;
	}
}
